# coding=utf-8
import sys
from selenium.webdriver.remote.webdriver import WebDriver
sys.path.append("D:/test/Appium2021")
from util.read_int import ReadIni
class GetByLocal:
    def __init__(self, driver: WebDriver, imgs):
        self.driver = driver
        self.imgs = imgs
    def get_element(self,key,section=None):
        #class>android.widget.Button
        read_int = ReadIni()
        local = read_int.get_value(key,section)
        if local != "None":
            by = local.split('>')[0] #切割>两边的元素拿取第一个值
            local_by = local.split('>')[1]
            try:
                if by =="id":
                    return self.driver.find_element_by_id(local_by)
                elif by =="class":
                    return self.driver.find_element_by_class_name(local_by)
                elif by =="xpath":
                    return self.driver.find_element_by_xpath(local_by)
                elif by == "uiautomator":
                    return self.driver.find_element_by_android_uiautomator(local_by)
            except:
                print(by+'没有找到元素')
                self.imgs.append(self.driver.get_screenshot_as_base64())
                # self.driver.save_screenshot("../jpg/test01.png")
                return None
        else:
            print("获取不到元素")
            return None
